// 测试产品排序功能
const axios = require('axios');

async function testProductSorting() {
    try {
        // 测试产品列表API
        const response = await axios.post('http://localhost:3000/api/products/list', {
            page: 1,
            pageSize: 10
        });

        console.log('API响应状态:', response.status);
        console.log('产品列表数据:');
        
        if (response.data && response.data.data && response.data.data.items) {
            const products = response.data.data.items;
            console.log(`总共获取到 ${products.length} 个产品`);
            
            products.forEach((product, index) => {
                console.log(`${index + 1}. ${product.name} - 状态: ${product.status} - 更新时间: ${product.updatedAt}`);
            });
            
            // 验证排序逻辑
            console.log('\n排序验证:');
            const activeProducts = products.filter(p => p.status === 'active');
            const inactiveProducts = products.filter(p => p.status === 'inactive');
            const draftProducts = products.filter(p => p.status === 'draft');
            
            console.log(`上架商品数量: ${activeProducts.length}`);
            console.log(`下架商品数量: ${inactiveProducts.length}`);
            console.log(`草稿商品数量: ${draftProducts.length}`);
            
            // 检查是否按预期排序（上架商品在前）
            let currentStatusPriority = 1;
            for (const product of products) {
                let statusPriority;
                if (product.status === 'active') statusPriority = 1;
                else if (product.status === 'draft') statusPriority = 2;
                else statusPriority = 3;
                
                if (statusPriority < currentStatusPriority) {
                    console.log('❌ 排序错误: 发现状态优先级倒序');
                    return;
                }
                currentStatusPriority = statusPriority;
            }
            console.log('✅ 排序正确: 上架商品优先显示');
            
        } else {
            console.log('❌ 没有获取到产品数据');
        }
        
    } catch (error) {
        console.error('测试失败:', error.response?.data || error.message);
    }
}

// 运行测试
testProductSorting(); 